package com.ehais.modbuswifi;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.ehais.util.OkHttpCallBack;
import com.ehais.util.OkHttpUtil;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;

import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;

public class MainLoginActivity extends AppCompatActivity {

    private EditText usernameEditText;
    private EditText passwordEditText;
    private Button loginButton;
    private ProgressBar loadingProgressBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_login);

        usernameEditText = (EditText) findViewById(R.id.username);
        passwordEditText = (EditText) findViewById(R.id.password);
        loginButton = (Button) findViewById(R.id.login);
        loadingProgressBar = (ProgressBar) findViewById(R.id.loading);

        usernameEditText.setText("jxvt@dw");
        passwordEditText.setText("Yue@1234");
        loginButton.setEnabled(true);

        loginButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String username=usernameEditText.getText().toString().trim();
                String password=passwordEditText.getText().toString().trim();
                if (username.length()==0 || password.equals("")){
                    Toast.makeText(MainLoginActivity.this,"请输入用户名", Toast.LENGTH_LONG).show();
                    return;
                }
                if(password.length()==0 || password.equals("")){
                    Toast.makeText(MainLoginActivity.this,"请输入密码", Toast.LENGTH_LONG).show();
                    return;
                }
                Map<String,String> map = new HashMap<String,String>();
                map.put("username",username);
                map.put("password",password);
                map.put("code","123");

                loadingProgressBar.setVisibility(View.VISIBLE);

                SharedPreferences sharedPreferences= getSharedPreferences("SharedData", Context.MODE_PRIVATE);
                SharedPreferences.Editor editor = sharedPreferences.edit();
                editor.remove("Authorization");
                editor.commit();
                OkHttpUtil.reset();

                OkHttpUtil.postEntity("/bai/login", map, new OkHttpCallBack() {

                    @Override
                    public void onSuccess(String json) {

                        try {
                            JSONObject jsonObject  = (JSONObject)new JSONTokener(json).nextValue();
                            if(jsonObject.has("token")){
                                Log.i("Login",jsonObject.getString("token"));
                                SharedPreferences sharedPreferences= getSharedPreferences("SharedData", Context.MODE_PRIVATE);
                                SharedPreferences.Editor editor = sharedPreferences.edit();
                                editor.putString("Authorization",jsonObject.getString("token"));
                                editor.commit();
                                OkHttpUtil.reset();

                                Intent intent = new Intent(MainLoginActivity.this, MainStationActivity.class);
                                intent.putExtra("data", "jiangyo");
                                //实行跳转
                                startActivity(intent);

                                MainLoginActivity.this.finish();

                            }
                        } catch (JSONException e) {
                            throw new RuntimeException(e);
                        }
                        loadingProgressBar.post(new Runnable() {
                            @Override
                            public void run() {
                                loadingProgressBar.setVisibility(View.GONE);
                            }
                        });

                    }
                });
            }
        });


    }
}